<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of YiiUpload
 *
 * @author ZangGuoPing
 */
class VodCmsUpload extends CApplicationComponent {

//put your code here
    public $minFileSize = 0;
    public $maxFileSize = 20000;
    public $saveType = 'system'; //system|ftp
    public $directory = 'upload';
    public $allowExt = array('jpg', 'gif', 'bmp', 'png', 'psd', 'rar', 'zip', 'sql', 'tar.gz', 'swf');
    public $imgExt = array('jpg', 'gif', 'bmp', 'png', 'psd'); //图片就建立独立的图片目录
    private $_errorCode = 0;
    private $_errorInfo = '';
    private $_appRoot;

    public function __construct() {
        $this->_appRoot = dirname(dirname(__FILE__));
    }

    public function upload($files) {
        if ($files['error'] > 0) {
            $this->_errorCode = $files['error'];
            switch ($files['error']) {
                case 1:
                    $this->_errorInfo = Yii::t('default', '文件太大！请不要上传超 %size%过的文件', array('size' => ini_get('upload_max_filesize')));
                    break;
                case 2:
                    $this->_errorInfo = Yii::t('default', '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');
                    break;
                case 3:
                    $this->_errorInfo = Yii::t('default', '文件上传不完整!请重新上传!');
                    break;
                case 4:
                    $this->_errorInfo = Yii::t('default', '没有选择上传文件！');
                    break;
            }
            return null;
        } else {
            $ext = substr(strrchr($files['name'], '.'), 1); //文件后缀名
            if (in_array($ext, $this->allowExt) === true) { //在允许的后缀名内
                if (is_readable($files['tmp_name']) === false) {
                    throw new Exception(Yii::t('default', '没有权限读取文件'), $files['tmp_name']);
                }
                $filesize = @filesize($files['tmp_name']);
                if ($filesize <= 0) {
                    throw new Exception(Yii::t('default', '文件尺寸不能为0'), $files['tmp_name']);
                }
                if (in_array($ext, $this->imgType)) {
                    $this->directory = 'upload' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . date('%Y/%m');
                } else {
                    $this->directory = 'upload' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . date('%Y/%m');
                }
                $filename = substr(md5_file($files['tmp_name']), 8, 16) . '.' . $ext; //新文件名称
                $destination = $this->directory . $filename; //目标文件地址
                if (strtolower($this->saveType) == 'ftp') { //采用FTP方式存储图片
                } else {
                    if (is_dir($this->directory) === false) { //目录不存在就创建
                        $directoryArr = explode(DIRECTORY_SEPARATOR, $this->directory); //已目录分隔符转为数组
                        $pathname = $this->_appRoot;
                        foreach ($directoryArr as $$directory) {
                            if (is_dir($pathname . DIRECTORY_SEPARATOR . $directory) == false) {//目录不存在就创建
                                $pathname.= DIRECTORY_SEPARATOR . $directory;
                                mkdir($pathname, substr(sprintf('%o', fileperms($pathname)), -4)); //修改用户权限
                            }
                        }
                    }
                    if (is_writable($this->directory) === false) { //检测目录是否有写的权限
                        throw new Exception(Yii::t('default', '%direcoty 没有文件写入权限', array('src' => $this->directory)));
                    } else {
                        if (!move_uploaded_file($files['tmp_name'], $destination)) {
                            throw new Exception(Yii::t('default', '移动上传文件到目标失败%src => $dist', array('src' => $files['tmp_name'], 'dist' => $destination)));
                        }
                    }
                }
            }
        }
    }

}

?>
